<template>
  <div class="todo-container">
    <div class="todo-wrap">
      <Header :addTodo="addTodo"/>
      <List :todos="todos" :deleteTodo="deleteTodo" :toggleTodo="toggleTodo"/>
      <Footer :todos="todos" :checkAll="checkAll" :clearCompleteTodos="clearCompleteTodos"/>
    </div>
  </div>
</template>

<script lang="ts">
  export default {
    name: 'Home'
  }
</script>

<script setup lang="ts">
import Header from "./components/Header.vue";
import List from "./components/List.vue";
import Footer from "./components/Footer.vue";
import { reactive, ref } from "vue";
import type { Todo, Todos } from "./types";

  // reactive({
  //   todos: []
  // })

  const todos = ref([
    {id: 1, title: 'AAA', complete: false},
    {id: 2, title: 'BBB', complete: true},
    {id: 3, title: 'CCC', complete: false},
  ])

  // 添加todo
  const addTodo = (title: string) => {
    const todo: Todo = {
      id: Date.now(),
      title,
      complete: false
    }
    todos.value.unshift(todo)
  }

  // 删除todo
  const deleteTodo = (id: number) => {
    todos.value = todos.value.filter(todo => todo.id !== id)
  }

  // 切换todo的勾选状态
  const toggleTodo = (todo: Todo) => {
    todo.complete = !todo.complete
  }

  // 全选或全不选
  const checkAll = (isCheck: boolean) => {
    todos.value.forEach(todo => todo.complete = isCheck)
  }

  // 清除已完成的
  const clearCompleteTodos = () => {
    todos.value = todos.value.filter(todo => !todo.complete)
  }

</script>

<style>
  body {
    background: #fff;
  }

  .btn {
    display: inline-block;
    padding: 4px 12px;
    margin-bottom: 0;
    font-size: 14px;
    line-height: 20px;
    text-align: center;
    vertical-align: middle;
    cursor: pointer;
    box-shadow: inset 0 1px 0 rgba(255, 255, 255, 0.2), 0 1px 2px rgba(0, 0, 0, 0.05);
    border-radius: 4px;
  }

  .btn-danger {
    color: #fff;
    background-color: #da4f49;
    border: 1px solid #bd362f;
  }

  .btn-danger:hover {
    color: #fff;
    background-color: #bd362f;
  }

  .btn:focus {
    outline: none;
  }

</style>
<style scoped>
.todo-container {
  width: 600px;
  margin: 0 auto;
}
.todo-container .todo-wrap {
  padding: 10px;
  border: 1px solid #ddd;
  border-radius: 5px;
}
</style>